package readColor.toImage;

import readColor.readColour;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
import java.util.List;

public class imageToArrayForColour {
    public static void main(String[] args) throws Exception {
        int x = 0;
        imageToArrayForColour rc = new imageToArrayForColour();
        x = rc.getScreenPixel(100, 345);
        System.out.println(x + " - ");
        rc.getImagePixe2("D:\\project\\IDEA\\readColour\\12.png");
    }
    public void getImagePixe2(String image) throws Exception {
        List<Rgbs> bilist = new ArrayList<Rgbs>();
        Integer[] rgb = new Integer[3];
        File file = new File(image);
        BufferedImage bi = ImageIO.read(file);//异常暂不处理
        int width = bi.getWidth();
        int height = bi.getHeight();
        int minx = bi.getMinX();
        int miny = bi.getMinY();
        //图片顶部的最大最小xy值
        int maxInx = 0;
        int maxInty = 0;
        int minIny = 0;
        int minInx = 0;
        //图片底部的最大最小xy值
        int maxBtx = 0;
        int maxBty = 0;
        int minBty = 0;
        int minBtx = 0;

        int ph = 0;
        Integer[] phrgb = new Integer[3];
        System.out.println("width=" + width + ",height=" + height + ".");
        System.out.println("minx=" + minx + ",miniy=" + miny + ".");
        for (int x = minx; x < width; x++) {
            for (int y = miny; y < height; y++) {
                int pixel = bi.getRGB(x, y); // 下面三行代码将一个数字转换为RGB数字
                rgb[0] = (pixel & 0xff0000) >> 16;
                rgb[1] = (pixel & 0xff00) >> 8;
                rgb[2] = (pixel & 0xff);
                if((rgb[0]>0&&rgb[0]<20)&&(rgb[1]>150&&rgb[1]<170)&&(rgb[2]>100&&rgb[2]<120)){
                    bilist.add(new Rgbs(x,y,rgb[0],rgb[1],rgb[2]));//分别表示x,y,rgb
                    //得到第一个最大x值和最大y值（第一个绿色的右下角顶点）
                    if (y<200){
                        if(maxInty == 0){
                            maxInty = y;
                            maxInx = x;
                        }else if(maxInty<=y){
                            maxInty = y;
                            maxInx = x;
                        }
                        //得到最小x
                        if(minInx == 0){
                            minInx = x;
                        }else if(minInx>=x){
                            minInx = x;
                        }
                        //得到最小y值
                        if(minIny == 0){
                            minIny = y;
                        }else if(minIny >= y){
                            minIny = y;
                        }
                    }else{
                        //得到底部最小x,以及y值
                        if(minBty == 0){
                            minBty =y;
                        }else if(minBty>=y){
                            minBty = y;
                            minBtx = x;
                            System.out.println(x+";;;;"+y);
                        }
                        //得到最大底部最大x值和y值
                        if(maxBtx==0){
                            maxBtx = x;
                        }else if(maxBtx<=x){
                            maxBtx = x;
                            maxBty = y;
                        }
                    }

                    System.out.println(x+";;"+y);
//                    if(x==155&&y==167){
//                        bi.setRGB(x, y, -0x00ffff);//标识位置 1677216
//                    }else{
//                        bi.setRGB(x, y, pixel);//标识位置 1677216
//                    }
                }
            }
        }
        //ph  = bi.getRGB(maxInx-25, maxInty+30);
//        phrgb[0] = (ph & 0xff0000) >> 16;
//        phrgb[1] = (ph & 0xff00) >> 8;
//        phrgb[2] = (ph & 0xff);

        //System.out.println(";;;;;"+bilist.size()+";;;"+bilist.get(1).getR()+","+bilist.get(1).getG()+","+bilist.get(1).getB());
        System.out.println("maxInx=="+maxInx+"maxInty=="+maxInty+"miny=="+minIny+"minx=="+minInx);
        System.out.println("maxBtx=="+maxBtx+"maxBty=="+maxBty+"minBty=="+minBty+"minBtx=="+minBtx);

        centerPoint centerPoint = new centerPoint();
        int xcenter = centerPoint.getCenterPointX(minInx,maxInx);
        int higntY = centerPoint.getCenterPointY(maxInty,minBty);
        coordinates coordinates = new coordinates();
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,1);
        ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,2);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,3);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,4);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,5);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,6);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,7);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,8);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,9);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,10);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,11);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,12);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,13);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,14);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,15);
        //ph  = bi.getRGB(coordinates.getX(), coordinates.getY());
       bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);
       //bi.setRGB(minBtx, minBty, -0x00ffff);
//        coordinates = centerPoint.getCoor(minInx,xcenter,higntY,minIny,16);
//        bi.setRGB(coordinates.getX(), coordinates.getY(), -0x00ffff);

//        phrgb[0] = (ph & 0xff0000) >> 16;
//        phrgb[1] = (ph & 0xff00) >> 8;
//        phrgb[2] = (ph & 0xff);
//        System.out.println(maxInty+";;;"+maxInx);
//        System.out.println("phrgb=="+phrgb[0]+","+phrgb[1]+","+phrgb[2]);


        FileOutputStream ops = new FileOutputStream(new File("D:\\project\\IDEA\\readColour\\1244.png"));
        ImageIO.write(bi,"png", ops);
        ops.flush();
        ops.close();

    }
    public int getScreenPixel(int x, int y) throws AWTException { // 函数返回值为颜色的RGB值。
        Robot rb = null; // java.awt.image包中的类，可以用来抓取屏幕，即截屏。
        rb = new Robot();
        Toolkit tk = Toolkit.getDefaultToolkit(); // 获取缺省工具包
        Dimension di = tk.getScreenSize(); // 屏幕尺寸规格
        System.out.println(di.width);
        System.out.println(di.height);
        Rectangle rec = new Rectangle(0, 0, di.width, di.height);
        BufferedImage bi = rb.createScreenCapture(rec);
        int pixelColor = bi.getRGB(x, y);

        return 16777216 + pixelColor; // pixelColor的值为负,加上颜色最大值就是实际颜色值。
    }
}
